"strict mode";

requirejs(['/lib/js-md5/src/md5.js'], function (md5) {
    $(() => {
        $('#loginForm').submit(function () {
            const account = $('#account').val()
            const password = md5(account + '/' + $('#password').val())
            console.log(password)
            $.ajax({
                url: "/api/login", method: 'post', async: true, dataType: "json",
                data: { account: account, password: password },
                success: function (result, _status, _xhr) {
                    if (result.code == 1) {
                        location.href = result.data.url;
                    } else {
                        alert(result.data.msg);
                    }
                },
                error: (xhr, _status, error) => {
                    console.error({ status: xhr.status, error: error, responseText: xhr.responseText })
                }
            });
            return false;
        });
        if ($('#loginForm').length == 0) {
            $.ajax({
                url: "/api/getLoginStatus", async: true, dataType: "json",
                success: function (result, _status, _xhr) {
                    console.log(result)
                    if (result.code == 1) {
                        $('.user-name').text(result.data.userName)
                        $('.user-icon').attr('src', result.data.icon)
                        $.ajax({
                            url: "/api/getMenu", async: true, dataType: "json",
                            success: function (result, _status, _xhr) {
                                const appendMenu = (c, i, a) => {
                                    let { url, name, icon, children, badge } = c;
                                    if (children !== undefined) {
                                        children = '<ul class="nav nav-treeview">\n' + children.map(appendMenu).join('\n') + '</ul>';
                                        name += '<i class="right fas fa-angle-left"></i>'
                                    }
                                    if (badge !== undefined) {
                                        badge = `<span class="right badge ${badge.class}">${badge.text}</span>`
                                    }
                                    return `<li class="nav-item"> <a href="${url ? url : "#"}" class="nav-link"><i class="nav-icon ${icon}"></i><p>${name}${badge ? badge : ""}</p></a>${children ? children : ""}</li>`
                                }
                                $('.sidebar nav ul').html(result.map(appendMenu).join('\n'));
                            },
                            error: (xhr, _status, error) => {
                                console.error({ status: xhr.status, error: error, responseText: xhr.responseText })
                            }
                        });
                    } else {
                        location.href = '/login.html';
                    }
                },
                error: (xhr, _status, error) => {
                    console.error({ status: xhr.status, error: error, responseText: xhr.responseText })

                }
            });
        }
    });

})